//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackYoYInflationCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
#include <gen/QL/Cashflows/InflationCoupon.h>
#include <gen/QL/Cashflows/YoYInflationCouponPricer.h>
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>^ capletVol) : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
    CYoYOptionletVolatilitySurface^ _CcapletVol;
    try
    {
#ifdef HANDLE
        _phBlackYoYInflationCouponPricer = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>::IsSome::get (capletVol))
        {
            _CcapletVol = safe_cast<CYoYOptionletVolatilitySurface^> (capletVol->Value);
            _CcapletVol->Lock();
        }
        Handle<QuantLib::YoYOptionletVolatilitySurface>& _capletVol = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>::IsSome::get (capletVol) ? static_cast<Handle<QuantLib::YoYOptionletVolatilitySurface>&> (_CcapletVol->GetHandle ()) : Handle<QuantLib::YoYOptionletVolatilitySurface>()); //1
        _ppBlackYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer> (new QuantLib::BlackYoYInflationCouponPricer ( _capletVol ));
        SetYoYInflationCouponPricer (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcapletVol != nullptr) _CcapletVol->Unlock();
    }
}
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer>& childNative, Object^ owner) : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackYoYInflationCouponPricer = NULL;
#endif
	_ppBlackYoYInflationCouponPricer = &childNative;
    _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
}
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (QuantLib::BlackYoYInflationCouponPricer& childNative, Object^ owner) : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackYoYInflationCouponPricer = NULL;
#endif
	_ppBlackYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer> (&childNative);
    _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
    _BlackYoYInflationCouponPricerOwner = owner;
    _YoYInflationCouponPricerOwner = owner;
}

Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (CBlackYoYInflationCouponPricer^ copy) : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackYoYInflationCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer> (copy->GetShared());
        _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
    }
}
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (System::Type^ t) : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackYoYInflationCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CBlackYoYInflationCouponPricer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (QuantLib::Handle<QuantLib::BlackYoYInflationCouponPricer>& childNative, Object^ owner)  : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
	_phBlackYoYInflationCouponPricer = &childNative;
	_ppBlackYoYInflationCouponPricer = &static_cast<boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer>>(childNative.currentLink());
    _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
    _BlackYoYInflationCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (QuantLib::Handle<QuantLib::BlackYoYInflationCouponPricer> childNative)  : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
	_phBlackYoYInflationCouponPricer = &childNative;
	_ppBlackYoYInflationCouponPricer = &static_cast<boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer>>(childNative.currentLink());
    _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::CBlackYoYInflationCouponPricer (QuantLib::BlackYoYInflationCouponPricer childNative)  : CYoYInflationCouponPricer(CBlackYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackYoYInflationCouponPricer = NULL;
#endif
	_ppBlackYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer> (new QuantLib::BlackYoYInflationCouponPricer (childNative));
    _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer));
}
#endif

Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::~CBlackYoYInflationCouponPricer ()
{
    if (_ppBlackYoYInflationCouponPricer != NULL)
    {
	    delete _ppBlackYoYInflationCouponPricer;
        _ppBlackYoYInflationCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::!CBlackYoYInflationCouponPricer ()
{
    if (_ppBlackYoYInflationCouponPricer != NULL)
    {
	    delete _ppBlackYoYInflationCouponPricer;
    }
}
QuantLib::BlackYoYInflationCouponPricer& Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::GetReference ()
{
    if (_ppBlackYoYInflationCouponPricer == NULL) throw gcnew NativeNullException ();
	return **_ppBlackYoYInflationCouponPricer;
}
boost::shared_ptr<QuantLib::BlackYoYInflationCouponPricer>& Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::GetShared ()
{
    if (_ppBlackYoYInflationCouponPricer == NULL) throw gcnew NativeNullException ();
	return *_ppBlackYoYInflationCouponPricer;
}
QuantLib::BlackYoYInflationCouponPricer* Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::GetPointer ()
{
    if (_ppBlackYoYInflationCouponPricer == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackYoYInflationCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackYoYInflationCouponPricer>& Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::GetHandle ()
{
	if (_phBlackYoYInflationCouponPricer == NULL)
	{
		_phBlackYoYInflationCouponPricer = new Handle<QuantLib::BlackYoYInflationCouponPricer> (*_ppBlackYoYInflationCouponPricer);
	}
	return *_phBlackYoYInflationCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer::HasNative () 
{
	return (_ppBlackYoYInflationCouponPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IBlackYoYInflationCouponPricer^ Cephei::QL::Cashflows::CBlackYoYInflationCouponPricer_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>^ capletVol)
{
    return gcnew CBlackYoYInflationCouponPricer ( capletVol);
}
